//
//  StoreIntroduceViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/9.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "StoreIntroduceViewModel.h"
#import "BaseTableViewCell.h"
#import "StoreBottomView.h"
#import "StoreNameTableViewCell.h"
#import "MainBusinessTableViewCell.h"
#import "AdvantageTableViewCell.h"
#import "SelfInforListTableViewCell.h"
#import "StoreCompanyInforTableViewCell.h"
#import "StoreIntroduceConnectsHeaderView.h"
#import "StoreIntroduceModel.h"
#import "PopupView.h"

@interface StoreIntroduceViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *selfTableView;
    NSArray *leftArr;
}
@property (nonatomic, strong) StoreIntroduceModel *model;

@property (nonatomic, assign) BOOL isOpen;

@end

@implementation StoreIntroduceViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super init]) {
        leftArr = @[@"业务联系人", @"手机", @"固话", @"传真", @"QQ"];
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(104);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.estimatedRowHeight = 30;
        tableView.rowHeight = UITableViewAutomaticDimension;
        [tableView registerClass:[StoreIntroduceConnectsHeaderView class] forHeaderFooterViewReuseIdentifier:@"StoreIntroduceConnectsHeaderView"];
        [superView addSubview:tableView];
        selfTableView = tableView;
        
        StoreBottomView * bottView = [[StoreBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(60)) inSuperView:superView];
        bottView.Block_Contact = ^(GLGWButton * _Nonnull actionBut) {
            PopupView * popWindowView = [[PopupView alloc] initPopupview:@[@"规格",@"经济"]];
            popWindowView.haveTitle = YES;
        };
    }
    return self;
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger cellNumber;
    switch (section) {
        case 0:{
            cellNumber = 1;
        }
            break;
        case 1:{
            cellNumber = 2;
            if (self.accountType == AccountType_FreeManagers) {
                cellNumber = 3;
            }
        }
            break;
        default:
            cellNumber = leftArr.count;
            break;
    }
    return cellNumber;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            StoreNameTableViewCell * cell_Name = [StoreNameTableViewCell cellWithTableView:tableView];
            if (self.model) {
                cell_Name.model = self.model;
            }
            cell = cell_Name;
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                MainBusinessTableViewCell * cell_Main = [MainBusinessTableViewCell cellWithTableView:tableView];
                [cell_Main setSepartionLineWith:lineModel_Half];
                if (self.model) {
                    cell_Main.model = self.model;
                }
                cell = cell_Main;
            } else if (indexPath.row == 1){
                AdvantageTableViewCell * cell_Advantage = [AdvantageTableViewCell cellWithTableView:tableView];
                [cell_Advantage setSepartionLineWith:lineModel_Half];
                if (self.model) {
                    cell_Advantage.model = self.model;
                }
                cell = cell_Advantage;
            } else {
                StoreCompanyInforTableViewCell * cell_company = [StoreCompanyInforTableViewCell cellWithTableView:tableView];
                if (self.model) {
                    cell_company.model = self.model;
                    cell_company.isOpen = self.isOpen;
                }
                cell = cell_company;
            }
        }
            break;
        case 2:{
            SelfInforListTableViewCell *cell_infor = [SelfInforListTableViewCell cellWithTableView:tableView];
            cell_infor.leftStr = leftArr[indexPath.row];
            if (self.model) {
                [cell_infor refreshCellWithModel:self.model index:indexPath];
            }
            cell = cell_infor;
            [cell setSepartionLineWith:lineModel_Half];
        }
            break;
        default:
            break;
    }
    if (cell == nil) {
        cell = [BaseTableViewCell cellWithTableView:tableView];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        StoreIntroduceConnectsHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreIntroduceConnectsHeaderView"];
        kWeakSelf(self);
        header.BlockOpenAll = ^(GLGWButton *btn) {
            weakself.isOpen = btn.selected;
            [weakself selctRow];
        };
        return header;
    }
    CGFloat headerHight = section == 0?0:kHeightProportion_In_iPhoneTriploid(10);
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, headerHight)];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        if (self.accountType == AccountType_FreeManagers) {
            return kHeightProportion_In_iPhoneTriploid(92);
        } else {
            return kHeightProportion_In_iPhoneTriploid(52);
        }
    }
    CGFloat headerHight = section == 0?0:kHeightProportion_In_iPhoneTriploid(10);
    return headerHight;
}

- (void)selctRow
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:1];
    [selfTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)setStoreId:(NSInteger)storeId
{
    _storeId = storeId;
    [self requestSoreInormation];
}

#pragma mark - 请求接口
// 店铺资料数据接口
- (void)requestSoreInormation
{
    if (self.userInforData.length == 0) return;
    NSString *str = [NSString stringWithFormat:@"%ld",self.storeId];
    NSDictionary * dic = @{@"shopId" : koString(@"1")};

    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kShopStoredetails] lodingView:nil lodingTextString:@"" isLoding:YES parameters:dic success:^(id  _Nullable response) {
        self.model = [StoreIntroduceModel yy_modelWithJSON:response];
        [self->selfTableView reloadData];
    } failure:^(NSError * _Nullable error) {}];
}

@end
